package org.ichamigua.user.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.ichamigua.user.service.UserInfoService;
import org.ichihamigua.common.framework.entity.Result;
import org.ichihamigua.common.user.dto.*;
import org.ichihamigua.common.user.vo.UserInfoVo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * @author rainkyzhong
 * @date 2023/10/30 0:31
 */
@RestController
@RequestMapping("/user_info")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    /**
     * 查看资料接口
     */
    @GetMapping("/details")
    public Result<UserInfoVo> details(HttpServletRequest request){
        Long userId = 1L;
        return userInfoService.details(userId);
    }


    /**
     * 修改个性签名接口
      */
    @PutMapping("/update/personal_signature")
    public Result<?> updatePersonalSignature(HttpServletRequest request, @RequestBody UpdatePersonalSignatureDto dto){
        Long userId = 1L;
        return userInfoService.updatePersonalSignature(userId, dto);
    }

    /**
     * 修改性别接口
     */
    @PutMapping("/update/gender")
    public Result<?> updateGender(HttpServletRequest request, @RequestBody UpdatePersonalGenderDto dto){
        Long userId = 1L;
        return userInfoService.updateGender(userId, dto);
    }

    /**
     * 修改生日接口
     */
    @PutMapping("/update/birthday")
    public Result<?> updateBirthday(HttpServletRequest request, @RequestBody UpdateBirthdayDto dto){
        Long userId = 1L;
        return userInfoService.updateBirthday(userId, dto);
    }

    //TODO 修改城市接口
    @PutMapping("/update/city")
    public Result<?> updateCity(HttpServletRequest request, @RequestBody UpdateCityDto dto){
        Long userId = 1L;
        return userInfoService.updateCity(userId, dto);
    }
    //TODO 修改封面接口
    @PutMapping("/update/background")
    public Result<?> updateBackground(HttpServletRequest request, MultipartFile file){
        Long userId = 1L;
        //检查文件是否是图片
        if(!Objects.equals(file.getContentType(), MediaType.IMAGE_JPEG_VALUE)
                &&!Objects.equals(file.getContentType(), MediaType.IMAGE_PNG_VALUE)
        ){
            return Result.fail("上传的不是图片");
        }
        //检查文件大小
        if(file.getSize() > 1024*1024*2){
            return Result.fail("图片不能大于2M");
        }
        return userInfoService.updateBackground(userId, file);
    }
}
